<!-- 渠道编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改渠道' : '添加渠道'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="渠道名称:" prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入渠道名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="渠道处理类:" prop="class_name">
        <el-input
          :maxlength="20"
          v-model="form.class_name"
          placeholder="请输入渠道处理类"
          clearable
        />
      </el-form-item>
      <el-form-item label="类型:" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio
            v-for="item in redios"
            :key="item.id"
            style="display: inline-block;"
            :label="item.id"
          >
            {{ item.name }}
          </el-radio>

          <!-- <el-radio :label="1">安卓</el-radio>
          <el-radio :label="2">苹果</el-radio> -->
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// import uploadImage from '@/components/uploadImage'

export default {
  name: "ChannelEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显 的数据
    data: Object
  },
  // components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 ,type:1}, this.data),
      redios: [
        { id: 1, name: "内部渠道" },
        { id: 2, name: "外部渠道" }
      ],
      // 表单验证规则
      rules: {
        name: [{ required: true, message: "请输入渠道名称", trigger: "blur" }],
        class_name: [{ required: true, message: "请输入渠道处理类", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "blur" }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        console.log(this.form,'form');
        this.form.type = this.form.type || 1;
        this.isUpdate = true;
      } else {
        console.log(this.form);
        this.form = {type:1};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/channel/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    }
  }
};
</script>

<style scoped></style>
